package com.mall.users.controller;

import com.mall.commons.utils.CommonResult;
import com.mall.model.domain.UmsMenu;
import com.mall.model.domain.UmsRole;
import com.mall.model.domain.UmsRoleMenuRelation;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.MenuVo;
import com.mall.users.service.UmsMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class UmsMenuController {

    @Autowired
    private UmsMenuService umsMenuService;

    /**
     * 树形结构返回所有菜单列表
     * @return
     */
    @GetMapping("/treeList")
    public CommonResult treeList() {
        List<MenuVo> list = umsMenuService.treeList();
        return CommonResult.success(list);
    }

    /**
     * 分页查询后台菜单
     * @return
     */
    @GetMapping("/list/{parentId}")
    public CommonResult list(@PathVariable Long parentId,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        CommonPage page = umsMenuService.list(parentId, pageNum, pageSize);
        return CommonResult.success(page);
    }

    /**
     * 添加后台菜单
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody UmsMenu umsMenu) {
        umsMenuService.create(umsMenu);
        return CommonResult.success(1);
    }

    /**
     * 根据ID删除后台菜单
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Long id) {
        umsMenuService.delete(id);
        return CommonResult.success(1);
    }


    /**
     * 根据ID获取菜单详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult getById(@PathVariable Long id) {
        UmsMenu umsMenu = umsMenuService.getById(id);
        return CommonResult.success(umsMenu);
    }

    /**
     * 修改后台菜单
     * @param id
     * @param umsMenu
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody UmsMenu umsMenu) {
        umsMenuService.update(umsMenu);
        return CommonResult.success(1);
    }

    /**
     * 修改角色状态
     * @return
     */
    @PostMapping("/updateHidden/{id}")
    public CommonResult updateHidden(@PathVariable Long id, Integer hidden) {
        umsMenuService.updateHidden(id,hidden);
        return CommonResult.success(1);
    }
}
